
if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready', plusReady, false);
}

var ws=null;
function plusReady() {
	
	console.log('index.html，开机启动了');
	var ts = plus.storage.getItem('ts');
	if(!ts){
		console.log('当前没有听书地址...................');
		plus.storage.setItem('ts','http://ting56.com/');
		ts = 'http://ting56.com/';
		
		plus.webview.prefetchURL(ts);
	}else if(ts.indexOf('ting')<=0){
		plus.storage.setItem('ts','http://ting56.com/');
		ts = 'http://ting56.com/';
		
		plus.webview.prefetchURL(ts);
	}
	
	ws = plus.webview.create(ts,'index');
	ws.show();
	
//	ws.onloaded = afterLoadUrl;
	ws.addEventListener('loaded',function(e){
		afterLoadUrl(e);
	});
	
	var upkeyc = 0;
	plus.key.addEventListener('volumeupbutton',function(e){
		console.log('按了音量向上,keycode:'+e.keyCode);
		upkeyc += e.keyCode;
		var vol = plus.device.getVolume();
		console.log('current vol='+vol);
		
		//按了两次了
		if((upkeyc/e.keyCode)%2==0 ){
			console.log('执行了两次音量向上键');
			ws.evalJS('javascript:FonHen_UpData()');
			upkeyc = 0;
			plus.device.setVolume(vol-0.14);
		}
		
		setTimeout(function(){
			console.log('非短时间按的都清零');
			upkeyc = 0;
		},500);
		
	});
	
	var downkeyc = 0;
	plus.key.addEventListener('volumedownbutton',function(e){
		console.log('按了音量向下,keycode:'+e.keyCode);
		
		downkeyc += e.keyCode;
		console.log('current downkeyc='+downkeyc);
		
		var vol = plus.device.getVolume();
		console.log('current vol='+vol);
		
		
		//按了两次了
		if((downkeyc/e.keyCode)%2==0){
			console.log('执行了两次音量向down键');
			ws.evalJS('javascript:FonHen_NextData()');
			downkeyc = 0;
			plus.device.setVolume(vol+0.14);
		}
		
		setTimeout(function(){
			console.log('非短时间按的都清零');
			downkeyc = 0;
		},500);
	});
	
	
	
}

function afterLoadUrl(e){
	loadingUrl();
	var url = ws.getURL()
	
	if(url && url != plus.storage.getItem('ts')){
		console.log('current url='+url+' ,把URL保存起来');
		plus.storage.setItem('ts',url);
	}
	
}

function loadingUrl(){
	console.log('预加载..................');
	ws.evalJS('$("*[id^=BAIDU_]").each(function(){$(this).html("")});');
	ws.evalJS('$("*[class^=qian]").each(function(){$(this).html("")});')
	ws.evalJS("(function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; bp.src = ''; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(bp, s);})();");
	ws.evalJS("(function () {var createElement = document.createElement;document.createElement = function (tag) {switch (tag) {case 'script':console.log('禁用动态添加脚本，防止广告加载');break;default:return createElement.apply(this, arguments);}}})();");
	
}

